package com.example.shiwu.service;

import android.content.Context;

import com.example.shiwu.db.dao.BoxSevenDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.BoxOne;
import com.example.shiwu.db.model.BoxSeven;
import com.example.shiwu.manager.DatabaseHelper;
import com.example.shiwu.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class BoxSevenService {

    private DatabaseHelper dbHelper;
    private BoxSevenDao<BoxSeven> BoxSevenDao;
    private ArrayList<BoxSeven> listBoxSeven = null;

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;

    public BoxSevenService() {
    }

    public BoxSevenService(Context context) {
        dbHelper = new DatabaseHelper(context);
    }


    /**获取单个数据
     * @param BoxSeven
     * @return
     */
    public List<BoxSeven> getBoxSeven(BoxSeven BoxSeven) {
        BoxSevenDao = BaseDaoFactory.getInstance().createBaseDao(BoxSevenDao.class, BoxSeven.class);
        listBoxSeven = new ArrayList<>();
        listBoxSeven.addAll(BoxSevenDao.query(BoxSeven));
        return listBoxSeven;
    }

    /**分页获取数据
     * @param BoxSeven
     * @return
     */
    public List<BoxSeven> getBoxSevenList(BoxSeven BoxSeven,Integer startIndex,Integer limit) {
        BoxSevenDao = BaseDaoFactory.getInstance().createBaseDao(BoxSevenDao.class, BoxSeven.class);
        listBoxSeven = new ArrayList<>();
        listBoxSeven.addAll(BoxSevenDao.query(BoxSeven,null,"create_time desc",null,startIndex,limit));
        return listBoxSeven;
    }


    public boolean save(BoxSeven BoxSeven) {
        BoxSevenDao = BaseDaoFactory.getInstance().createBaseDao(BoxSevenDao.class, BoxSeven.class);
        long size = BoxSevenDao.insert(BoxSeven);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public boolean saveOrUpdata(BoxSeven BoxSeven) {
        BoxSevenDao = BaseDaoFactory.getInstance().createBaseDao(BoxSevenDao.class, BoxSeven.class);
        BoxSeven boxSeven = new BoxSeven();
        boxSeven.setItemId(BoxSeven.getItemId());
        List<BoxSeven> lis =BoxSevenDao.query(boxSeven);
        Boolean bl = true;
        if(lis.size()>0){
            BoxSeven.setUpdateTime(DateUtils.parseDate());
            long size = BoxSevenDao.update(BoxSeven,new BoxSeven());
            if(size >0 ){
                return bl;
            }
            bl = false;
        }else{
            BoxSeven.setUpdateTime(DateUtils.parseDate());
            long size = BoxSevenDao.insert(BoxSeven);
            if(size >0 ){
                return bl;
            }
            bl = false;
        }

        return bl;
    }

    public boolean updade(BoxSeven BoxSeven,BoxSeven where) {
        long size = BoxSevenDao.update(BoxSeven,where);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public Boolean delete(BoxSeven boxSeven) {
        int size = BoxSevenDao.delete(boxSeven);
        if(size >0 ){
            return true;
        }
        return false;
    }
}
